package com.spark.tags;

import java.util.Iterator;
import java.util.List;

import com.spark.core.SparkBuilder;
import com.spark.core.SparkContext;
import com.spark.core.XMLOutputter;
import com.spark.core.XMap;

/**
 * 没有命名空间的tag,默认用这个tag实例.
 * 
 * @version $Id: XMLTag.java v 1.0 2010-12-19 下午08:44:13 IBM Exp $
 * @author <a href="mailto:njq.niu@hand-china.com">vincent</a>
 */
public class XMLTag extends SparkTag {

	public String execute(XMap view, SparkContext context) throws Exception {
		StringBuilder sb = new StringBuilder();
		List<XMap> children = view.getChilds();
		String name = view.getName();
		sb.append("<").append(name);
		XMLOutputter.getAttributeXML(view, sb);
		String text = view.getText();
		if (children == null && text == null) {
			sb.append("/>");
		} else {
			sb.append(">");
		}
		if (children != null) {
			Iterator<XMap> it = children.iterator();
			while (it.hasNext()) {
				XMap child = it.next();
				sb.append(SparkBuilder.build(child, context));
			}
		} else if(text!=null){
			sb.append(text);
		}
		sb.append("</").append(name).append(">");
	
		return sb.toString();
	}
}
